//
//  HWTabBarController.m
//  嗨文
//
//  Created by 响当当 on 2019/1/8.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWTabBarController.h"
#import "RealTimeViewController.h"
#import "ServiceViewController.h"
#import "MeViewController.h"
#import "NewReportViewController.h"

@interface HWTabBarController ()
@property (nonatomic, copy)NSArray *infoArray;
@end

@implementation HWTabBarController

- (NSArray *)infoArray
{
    if (!_infoArray)
    {
        _infoArray = @[@{@"className": @"RealTimeViewController",
                         @"titleName": @"实时",
                         @"image": @"shishi"},
                       @{@"className": @"ServiceViewController",
                         @"titleName": @"服务中",
                         @"image": @"fuwuzhong"},
                       @{@"className": @"NewReportViewController",
                         @"titleName": @"报告",
                         @"image": @"baogao"},
                       @{@"className": @"MeViewController",
                         @"titleName": @"我的",
                         @"image": @"wode"}];
    }
    return _infoArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSMutableArray *viewControllers = [NSMutableArray array];
    
    for (NSDictionary *dict in self.infoArray)
    {
        UIViewController *vc = [[NSClassFromString(dict[@"className"]) alloc] init];
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vc];
        
        navi.tabBarItem.title = dict[@"titleName"];
        navi.tabBarItem.image = [[UIImage imageNamed:[NSString stringWithFormat:@"%@1",dict[@"image"]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        navi.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@2", dict[@"image"]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        [viewControllers addObject:navi];
    }
    
    self.viewControllers = viewControllers;
    self.selectedViewController = [self.viewControllers objectAtIndex:0];
    
     if (@available(iOS 13.0, *)) {
            // 修改设置
            UITabBarAppearance *appearance = [UITabBarAppearance new];
            // 设置未被选中的颜色
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor], NSBackgroundColorAttributeName: [UIColor redColor]};
            // 设置被选中时的颜色
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor redColor], NSBackgroundColorAttributeName: [UIColor greenColor]};
        }
    else
    {
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:LCHexColor(0xF54337)}
                                                    forState:UIControlStateSelected];
    }
   
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    if ([tabBarController.viewControllers containsObject:tabBarController.selectedViewController]) {
        if ([viewController isEqual:tabBarController.selectedViewController]) {
            NSLog(@"这里执行操作");
            return NO;
        }
    }
    return YES;
}
@end
